package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import com.hzit.util.ResponseUtil;
import com.hzit.vo.StudentVo;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
	private IClassesService classesService;

	//1. 在servlet开始加载时调用此方法，从spring容器中得到IStudentService对象
	@Override
	public void init() throws ServletException {
		//在servlet容器中得到spring容器：
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		//从spring容器中得到IStudentService
		classesService = wac.getBean(IClassesService.class);
	}

	//2. 每次请求都要调用此方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		List<Classes> classesList = classesService.findAll();
		ResponseUtil.out(resp,R.ok(classesList));

	}


}
